*===============================================================================
*Firm and Country Embedded Productivity (Alviarez, Cravino and Ramondo) 
*JPE revision exercises
*Last Modified by Vanessa Alviarez 06/2022
*===============================================================================
global typeden=1
include "TFP_firm_directory_historical.do"
set memory 64g
global lf "LF"


*List of exercises: 
*Exercise 1: Scale effect using the number of firms in Orbis. This requires to know the total number of firms per country in Orbis and compute the phi using the Orbis denominator (this last part is done). Our beta is 0.2. 
*Exercise 2: Phi by age. Bring the date of incorporation. DO a version where you replace by the average age whenever the date of incorporation was not available. I guess we can use the oldest affiliate to determine the age of the MNCs
*Exercise 3.1: Exports. Substract exports from numerator and denominator in ORBIS (for subsample). See what survives. Combinations (a) Keep only firms in manuf with export observations, and replace with zeros (b) Replace all missings by zeros, (c) present manuf and services together and separately.  
*Exercise 3.2: Exports. Show that the adjustment (differences in export intensities between MNCs and all economy) are small enough (small adjustment or correction factor)

*Aggregates: (1) Compare the total revenue and employment in Orbis with the rev/emp reported in KLEMS. --- additional coverage figure. --- Check the code for the MNCs coverage as well
*Aggregates: (2) Compare the export intensity of MNCs versus the export intensity of the entire economy (by sector-country---this will help 3.1). Domestic sales MNCs/Total sales MNCs and Domestic sales all firms/Total sales all firms. This is using OECD/KLEMS dataset. Notice that OECD has only information for very few countries on this. 

*** NOTES ***
*Prepare a folder with the final data/code/results in order 
*Check the Xnn in the counterfactual dta file. (from the code) Xnn (revenue by local producers, this is excluding the sales of foreign affiliates) ----- ynn/yii -----
*We compute aggregate exports from WIOT (world input-output tables). Because of the lack of consistent information in OECD. Indeed KLEMS do not offer information on exports. 
* How I call in the locals the different aggregate files (new, old, etc)
*Note: here we can already compute the "Domestic sales all firms/Total sales all firms" for the entire economy ***
*** so where Xnn is calculated, and where sin are declare in the file???


*Aggregates: (2) Compare the export intensity of MNCs versus the export intensity of the entire economy (by sector-country---this will help 3.1). Domestic sales MNCs/Total sales MNCs and Domestic sales all firms/Total sales all firms. This is using OECD/KLEMS dataset. Notice that OECD has only information for very few countries on this. 
*I need to review the exports of MNCs of Japan and Sweeden 
*-----------------------------------------------------------------
clear all
use "${output}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear
keep year isocode sector* exports EXP_usd_mnc GO_usd GO_usd_mnc
order year isocode sector1 sector GO_usd exports GO_usd_mnc EXP_usd_mnc
drop if sector1== sector

collapse (sum) GO_usd exports GO_usd_mnc EXP_usd_mnc, by(year isocode sector1)
keep if year==2013 | year==2016
keep if isocode=="CA" |  isocode=="EE" | isocode=="ES" | isocode=="IT" | isocode=="JP" | isocode=="SE" | isocode=="US" 

*Domestic intensity 
*keep if sector1=="Manufacturing (C)"
*keep if sector1=="Manufacturing (C)" | sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)"
*drop if sector1=="Non-Market Economy"

gen xx=exports/GO_usd
gen yy=EXP_usd_mnc/GO_usd_mnc

gen economy= 1-(exports/GO_usd)
gen mnc= 1-(EXP_usd_mnc/GO_usd_mnc)
gen ratio=economy/mnc
**keep if ratio!=. & ratio!=0
sort  year isocode sector1

*sum xx yy, d
*keep if xx<=1 & yy<=1
*keep if xx!=0 & yy!=0
drop xx yy 

tempfile temp
save `temp', replace


*---------------------------------
clear all
use "${output}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear
keep year isocode sector* exports EXP_usd_mnc GO_usd GO_usd_mnc
order year isocode sector1 sector GO_usd exports GO_usd_mnc EXP_usd_mnc
drop if sector1== sector

collapse (sum) GO_usd exports GO_usd_mnc EXP_usd_mnc, by(year isocode sector1)
keep if year==2013 | year==2016
keep if isocode=="CA" |  isocode=="EE" | isocode=="ES" | isocode=="IT" | isocode=="JP" | isocode=="SE" | isocode=="US" 

collapse (sum) GO_usd exports GO_usd_mnc EXP_usd_mnc, by(year isocode)
gen sector1="Total"

gen xx=exports/GO_usd
gen yy=EXP_usd_mnc/GO_usd_mnc

gen economy= 1-(exports/GO_usd)
gen mnc= 1-(EXP_usd_mnc/GO_usd_mnc)
gen ratio=economy/mnc
sort  year isocode sector1
drop xx yy 

append using `temp'
order year isocode sector1 GO_usd exports GO_usd_mnc EXP_usd_mnc economy mnc ratio
sort  year isocode sector1

*Bring the MNCs market shares relative to FR
*---------------------------------
gen type="NA"
replace type="total" if sector1=="Total"
replace type="manuf" if sector1=="Manufacturing (C)"
replace type="serv" if sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)"
replace type="others" if sector1=="Other_Goods (A-B-D-E-F)"
tab type

rename year year1
gen year=2016
merge m:1 year isocode type using "${output}/aff_mkt_shares.dta"
drop if _merge==2
drop _merge
drop year
rename year1 year
sort  year isocode sector1
order year isocode sector1 type


*Exercise 1: Scale effect using the number of firms in Orbis. This requires to know the total number of firms per country in Orbis and compute the phi using the Orbis denominator (this last part is done). Our beta is 0.2. 
*The scale effect is == baseline (using Orbis as denominator, instead of KLEMS) - the log of the numer of firms relative to France (used to compute the denominator from Orbis)
*Here I think we want to repeat the exercise using population instead. But also using alternative measures of the number of firms (from OECD STAN)
*Aggregates: (1) Compare the total revenue and employment in Orbis with the rev/emp reported in KLEMS. --- additional coverage figure. --- Check the code for the MNCs coverage as well
*-----------------------------------------------------------------

*Compare the total revenue and employment in Orbis with the rev/emp reported in KLEMS. --- additional coverage figure. --- Check the code for the MNCs coverage as well
*--------------------------------
clear all
use year isocode sector1 sector gdp_gr data_source GO_usd EMPE using "${output}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear
rename GO_usd GO_usd_base
rename EMPE EMPE_base
merge m:1 year sector isocode using "${output}/klems_oecd_comb_selectvar_v2.dta", keepusing(GO_usd EMPE)
drop _merge
rename GO_usd GO_usd_new
rename EMPE EMPE_new

sum GO_usd_base GO_usd_new
sum EMPE_base EMPE_new

merge m:1 year sector isocode using "${output}/orbis_historical_agg_allyears_allvariables.dta", keepusing(GO_usd_orbis_all EMP_orbis_all)
drop if _merge==2
drop _merge

drop if sector==sector1
keep if year==2016
keep if isocode!=""
keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 

tempfile 
save `temp', replace 

*--------------------------------------------------------------------
use `temp', clear
collapse (sum) GO* EMP* (mean) gdp_gr, by(year isocode)
format %9.0fc  GO* EMP*

gen GO_share_orbis= GO_usd_orbis_all/GO_usd_new
gen EMP_share_orbis= EMP_orbis_all/EMPE_new
sum GO_share_orbis

graph hbar GO_share_orbis, bar(1, color(red) fcolor(none) lwidth(medium))  yline(0) over( isocode , sort ( GO_share_orbis ) descending label(labsize(large)))  legend( region(lwidth(none)) size(large)  pos(6) label(1 "")) ytitle("") title("All economy")  scale(*.5) graphregion(color(white))

*--------------------------------------------------------------------
use `temp', clear
collapse (sum) GO* EMP* (mean) gdp_gr, by(year isocode sector1)
format %9.0fc  GO* EMP*

gen GO_share_orbis= GO_usd_orbis_all/GO_usd_new
gen EMP_share_orbis= EMP_orbis_all/EMPE_new
sum GO_share_orbis

graph hbar GO_share_orbis if sector1=="Manufacturing (C)", bar(1, color(red) fcolor(none) lwidth(medium))  yline(0) over( isocode , sort ( GO_share_orbis ) descending label(labsize(large)))  legend( region(lwidth(none)) size(large)  pos(6) label(1 "")) ytitle("") title("Manufacturing")  scale(*.5) graphregion(color(white))

graph hbar GO_share_orbis if sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)" , bar(1, color(red) fcolor(none) lwidth(medium))  yline(0) over( isocode , sort ( GO_share_orbis ) descending label(labsize(large)))  legend( region(lwidth(none)) size(large)  pos(6) label(1 ""))  ytitle("") title("Services")  scale(*.5) graphregion(color(white))

graph hbar GO_share_orbis if sector1=="Manufacturing (C)" | sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)" , bar(1, color(red) fcolor(none) lwidth(medium))  yline(0) over( isocode , sort ( GO_share_orbis ) descending label(labsize(large)))  legend( region(lwidth(none)) size(large)  pos(6) label(1 ""))  ytitle("") title("Manuf+Serv")  scale(*.5) graphregion(color(white))

graph hbar GO_share_orbis if sector1!="Manufacturing (C)" & sector1!="Market_Services (G-H-I-J-K-M-N-R-S-T)" , bar(1, color(red) fcolor(none) lwidth(medium))  yline(0) over( isocode , sort ( GO_share_orbis ) descending label(labsize(large)))  legend( region(lwidth(none)) size(large)  pos(6) label(1 ""))  ytitle("") title("All but Manuf+Serv")  scale(*.5) graphregion(color(white))





*Scale effect (Aggregate) 
*---------------------------------------------------------------------------------
clear all
use year isocode type D_A using "${output}/estimates_agg_naics_sales_s4_base_woparent_orbis_all.dta", clear
merge 1:1 year isocode type using "${output}/estimates_database_v2.dta", keepusing(DA_base)
drop _merge

gen DA_base_phi=(-1)*DA_base*(0.2)
gen DA_orbis_phi=(-1)*D_A*(0.2)


local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"
local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n,orbis}"
global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter DA_base_phi DA_orbis_phi, sort $opt1  mcolor(red)) (lfit DA_base_phi DA_orbis_phi, lcolor(red)) (line DA_base_phi DA_orbis_phi, lcolor(none)) ,  ylabel(-0.6(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-0.6(0.2)0.2, format(%5.1f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 


*Let's construct PHI
corr DA_orbis_phi DA_base_phi
corr DA_orbis_phi DA_base_phi if isocode!="MX"

*Let's bring information on the number of firms (per country)
preserve
clear all
use "${output}/orbis_historical_agg_allyears_allvariables.dta", clear
keep isocode year sector number_turm_orbis_all number_emp_orbis_all
keep if year==2016
drop if sector=="NA"
collapse (sum) number_turm_orbis_all number_emp_orbis_all, by(isocode year)
sum number_turm_orbis_all number_emp_orbis_all, d
tempfile temp
save `temp', replace
restore

merge 1:1 isocode year using `temp'
drop if _merge==2
drop _merge

gen xx=number_turm_orbis_all if isocode=="FR"
by year, sort: egen yy=max(xx)
gen scale=ln(number_turm_orbis_all/yy)
drop xx yy 
gen phi_avg=DA_orbis_phi-0.2*(scale)
rename DA_orbis_phi phi_orbis
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

keep year isocode type phi_orbis scale phi_avg 
order year isocode type phi_orbis scale phi_avg 
label variable scale "Scale: ln(number_firms_orbis_aggregate_cty/number_firms_orbis_aggregate_fra)"
save "${output}/scale_effect_aggregate.dta", replace 
 
 

** Scale figure (residualized) **
*--------------------------------------
**drop if isocode=="MX"
local grvar0 "gdp_pw_ppp"
local LHS1 DA_orbis_phi
local RHS ln_`grvar0'
local RHS2 scale

	
sum `LHS1' `RHS'  `LHS2' 
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0305")'{sub:n}"
*, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"

reg `LHS1' `RHS' 
reg `LHS1' `RHS' `RHS2'

sum `LHS1'
reg `LHS1' `RHS2', nocon
mat b = e(b)
mat V = e(V)
global c_first: display %-03.2fc round(b[1,1],0.0001)
global se_first: display %-03.2fc round(sqrt(V[1,1]),0.0001)
predict lhs, res

reg `RHS' `RHS2', nocon 
mat b = e(b)
mat V = e(V)
global c_second: display %-03.2fc round(b[1,1],0.0001)
global se_second: display %-03.2fc round(sqrt(V[1,1]),0.0001)
predict rhs, res

reg lhs rhs 
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)
display "$c1"


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter lhs rhs, sort $opt1  mcolor(red)) (lfit lhs rhs, lcolor(red)) (line lhs lhs, lcolor(none)) ,  ylabel(-0.4(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0.4, format(%5.1f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`ytitle': $c1 ($se1)") rows(1) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 


	

*Scale effect (Manufacturing) 
*---------------------------------------------------------------------------------	
clear all
use year isocode type D_A using "${output}/estimates_aggtype_naics_sales_s4_base_woparent_orbis_all.dta", clear
rename D_A xx
merge 1:1 year isocode type using "${output}/estimates_aggtype_naics_sales_s4_base_woparent.dta", keepusing(D_A)
drop _merge
rename D_A DA_base
rename xx D_A

gen DA_base_phi=(-1)*DA_base*(0.2)
gen DA_orbis_phi=(-1)*D_A*(0.2)

keep if type=="manuf"

*Let's bring information on the number of firms (per country)
preserve

clear all
use "${output}/orbis_historical_agg_allyears_allvariables.dta", clear
keep isocode year sector number_turm_orbis_all number_emp_orbis_all
keep if year==2016
drop if sector=="NA"
gen type="NA"
replace type="manuf" if sector=="Basic Metals (24-25)" | sector=="ChePetPla (19-23)" | sector=="ElecMach (26-28)" | sector=="Food (10-12)" | sector=="TexWood (13-18)" | sector=="TranspOtherManuf (29-33)"
collapse (sum) number_turm_orbis_all number_emp_orbis_all, by(isocode year type)
sum number_turm_orbis_all number_emp_orbis_all, d
tempfile temp
save `temp', replace

restore

merge m:1 isocode year type using `temp'
drop if _merge==2
drop _merge

gen xx=number_turm_orbis_all if isocode=="FR"
by year, sort: egen yy=max(xx)
gen scale=ln(number_turm_orbis_all/yy)
drop xx yy 
gen phi_avg=DA_orbis_phi-0.2*(scale)
rename DA_orbis_phi phi_orbis

keep year isocode type phi_orbis scale phi_avg 
order year isocode type phi_orbis scale phi_avg 
label variable scale "Scale: ln(number_firms_orbis_manuf_cty/number_firms_orbis_manuf_fra)"
save "${output}/scale_effect_manufacturing.dta", replace 
 	
		
	
	
	



*Exercise 2: Phi by age. Bring the date of incorporation. DO a version where you replace by the average age whenever the date of incorporation was not available. I guess we can use the oldest affiliate to determine the age of the MNCs
*Again is the age of the MNC (not the age of affiliates) -- Check what is the point in the replies that we are doing with this
*-----------------------------------------------------------------
clear all
local edad=15
use "${output}/estimates_agg_naics_sales_s4_base_aboveage`edad'_all.dta", clear

local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
	
sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local title "Affiliates older than `edad' years"
local title ""

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0, format(%5.1f) labsize(medlarge)) ///
	title("`title'", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 
graph export "${results}/figures/fig_aboveage`edad'.pdf", replace
	
	


	
*-----------------------------------------------------------------
*Exercise 3.1: Exports. Substract exports from numerator and denominator in ORBIS (for subsample). See what survives. Combinations (a) Keep only firms in manuf with export observations, and replace with zeros (b) Replace all missings by zeros, (c) present manuf and services together and separately.
*-----------------------------------------------------------------

** Aggregate: Keeping the same of sectors the same (using only countries for which we have the same sectors as in the baseline) 
*----------------------------------------------------
clear all
use year isocode type sector1 sector DA num_aff num_aff_ctry num_parents using "${output}/estimates_sec_naics_sales_exp_s4_base_exclexp_alt_all.dta", clear
rename DA xx
merge 1:1 year isocode type sector1 sector using "${output}/estimates_sec_naics_sales_s4_base_woparent.dta", keepusing(DA)
rename DA D_base
rename xx D_A

gen a=(D_base!=. &  D_A!=.)

by isocode year, sort: egen xx_baseline=count(D_base)
by isocode year, sort: egen xx_exp=total(a)
tab isocode if xx_baseline==xx_exp

gen dummy=0
foreach xx in DE EE FR GB GR HR HU SI {
display "`xx'"
replace dummy=1 if isocode=="`xx'"
}
keep if dummy==1

*------------------------------------------------
clear all
use "${output}/estimates_agg_naics_sales_exp_s4_base_exclexp_alt_all.dta", clear
rename D_A xx
rename D_A_se xx_se
merge 1:1 year isocode type using "${output}/estimates_agg_naics_sales_s4_base_woparent.dta", keepusing(D_A D_A_se)
drop _merge
rename D_A DA_base
rename D_A_se D_A_se_base
rename xx D_A
rename xx_se D_A_se

gen dummy=0
foreach xx in DE EE FR GB GR HR HU SI {
display "`xx'"
replace dummy=1 if isocode=="`xx'"
}
keep if dummy==1

gen DA_base_phi=(-1)*DA_base*(0.2)
gen DA_exp_phi=(-1)*D_A*(0.2)
gen DA_base_phi_se=(-1)*D_A_se_base*(0.2)
gen DA_exp_phi_se=(-1)*D_A_se*(0.2)

local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"
local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n,exp}"
global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter DA_base_phi DA_exp_phi, sort $opt1  mcolor(red)) (line DA_base_phi DA_base_phi, lcolor(red)) ,  ylabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) ///4
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 
*** before july 1 ****** graph export "${results}/figures/fig_exclexports_aggregate.pdf", replace
***graph export "${results}/figures/fig_exclexports_aggregate_replacemissing.pdf", replace
****graph export "${results}/figures/fig_exclexports_aggregate_replacemissing_2.pdf", replace
*_2 refers to substracting exports in the numerator and denominator only when exports are non-missing and non-zero
*graph export "${results}/figures/fig_exclexports_aggregate_replacemissing_fvalt2.pdf", replace
graph export "${results}/figures/fig_exclexports_aggregate_replacemissing_fvalt3.pdf", replace


***Alternative figure with confidence intervals***	
gen u_DA_base = DA_base_phi + 1.96 * DA_base_phi_se 
gen l_DA_base = DA_base_phi - 1.96 * DA_base_phi_se 
gen u_DA = DA_exp_phi + 1.96 * DA_exp_phi_se 
gen l_DA = DA_exp_phi - 1.96 * DA_exp_phi_se 

keep year isocode type u_DA_base l_DA_base u_DA l_DA DA_base_phi DA_exp_phi
foreach vv in u_DA_base l_DA_base u_DA l_DA DA_base_phi DA_exp_phi {
display "`vv'"
}
gsort DA_base_phi
drop year
gen t=_n
sum t

sum t
forvalues ii=1(1)`r(max)' {
local ctry`ii' = isocode[`ii']
local obs`ii' = `ii'
display "`ctry`ii'' ; `obs`ii''"
}

twoway rcap u_DA_base l_DA_base t, lc(red) /// 
||     rcap u_DA l_DA t, lc(blue) /// 
||     scatter DA_base_phi t, mc(red)  /// 
||     scatter DA_exp_phi t, mc(blue)  ///
yline(0) yline(0) ylab(-0.8(0.2)0.2) xlab(1(1)8)  legend(off) xlabel(, valuelabel) xlabel(, angle(45)) ytitle("") xtitle("") graphregion(fcolor(white) lcolor(white)) xlabel(`obs1' "`ctry1'" `obs2' "`ctry2'"  `obs3' "`ctry3'" `obs4' "`ctry4'" `obs5' "`ctry5'" `obs6' "`ctry6'" `obs7' "`ctry7'" `obs8' "`ctry8'", labsize(small)) 
display "fig1_`title'_`yy'"
*graph export "${results}/figures/fig_exclexports_aggregate_replacemissing_fvalt2_b.pdf", replace
graph export "${results}/figures/fig_exclexports_aggregate_replacemissing_fvalt3_b.pdf", replace


***** Parentesis 
*** I will need to compute affiliate's revenues from Orbis and compare with OECD revenues (maybe is too OFF in EE)
***** Parentesis 


*NEW AGGREGATE: adjusting the baseline to have the same firms as the one excluding exports
*--------------------------------------------------------------------------
clear all
use "${output}/estimates_agg_naics_sales_exp_s4_base_exclexp_alt_all.dta", clear
rename D_A xx
rename D_A_se xx_se
merge 1:1 year isocode type using "${output}/estimates_agg_naics_sales_s4_base_check_all.dta", keepusing(D_A D_A_se)
drop _merge
rename D_A DA_base
rename D_A_se D_A_se_base
rename xx D_A
rename xx_se D_A_se

gen dummy=0
foreach xx in DE EE FR GB GR HR HU SI {
display "`xx'"
replace dummy=1 if isocode=="`xx'"
}
keep if dummy==1

*gen DA_base_phi=(-1)*DA_base*(0.2)
*gen DA_exp_phi=(-1)*D_A*(0.2)
*gen DA_base_phi_se=(-1)*D_A_se_base*(0.2)
*gen DA_exp_phi_se=(-1)*D_A_se*(0.2)

gen DA_base_phi=(1)*DA_base
gen DA_exp_phi=(1)*D_A
gen DA_base_phi_se=(1)*D_A_se_base
gen DA_exp_phi_se=(1)*D_A_se

local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"
local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n,exp}"
global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter DA_base_phi DA_exp_phi, sort $opt1  mcolor(red)) (line DA_base_phi DA_base_phi, lcolor(red)) ,  ylabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) ///4
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 
***graph export "${results}/figures/fig_exclexports_aggregate_replacemissing_fvalt2_adj.pdf", replace


***Alternative figure with confidence intervals***	
gen u_DA_base = DA_base_phi + 1.96 * DA_base_phi_se 
gen l_DA_base = DA_base_phi - 1.96 * DA_base_phi_se 
gen u_DA = DA_exp_phi + 1.96 * DA_exp_phi_se 
gen l_DA = DA_exp_phi - 1.96 * DA_exp_phi_se 

keep year isocode type u_DA_base l_DA_base u_DA l_DA DA_base_phi DA_exp_phi
foreach vv in u_DA_base l_DA_base u_DA l_DA DA_base_phi DA_exp_phi {
display "`vv'"
}
gsort DA_base_phi
gen num=1 if isocode=="SI"
replace num=2 if isocode=="EE"
replace num=3 if isocode=="HR"
replace num=4 if isocode=="GR"
replace num=5 if isocode=="HU"
replace num=6 if isocode=="GB"
replace num=7 if isocode=="FR"
replace num=8 if isocode=="DE"
**gsort -num
drop year
gen t=_n
sum t

sum t
forvalues ii=1(1)`r(max)' {
local ctry`ii' = isocode[`ii']
local obs`ii' = `ii'
display "`ctry`ii'' ; `obs`ii''"
}


twoway rcap u_DA_base l_DA_base t, lc(red) /// 
||     rcap u_DA l_DA t, lc(blue) /// 
||     scatter DA_base_phi t, mc(red)  /// 
||     scatter DA_exp_phi t, mc(blue)  ///
yline(0) yline(0) ylab(-2(1)4) xlab(1(1)8)  xlabel(, valuelabel) xlabel(, angle(45)) ytitle("") xtitle("") graphregion(fcolor(white) lcolor(white)) xlabel(`obs1' "`ctry1'" `obs2' "`ctry2'"  `obs3' "`ctry3'" `obs4' "`ctry4'" `obs5' "`ctry5'" `obs6' "`ctry6'" `obs7' "`ctry7'" `obs8' "`ctry8'", labsize(small)) legend(order(3 "Baseline (adjusted)" 4 "Export correction") rows(1) size(small) bplace(n) ring(0) region(lwidth(none)))
display "fig1_`title'_`yy'"
*graph export "${results}/figures/fig_exclexports_aggregate_replacemissing_fvalt2_adj_b.pdf", replace
graph export "${results}/figures/fig_exclexports_aggregate_replacemissing_fvalt2_adj_c.pdf", replace






	
*Manufacturing (exports): Keeping the same of sectors the same (using only countries for which we have the same sectors as in the baseline) 
*--------------------------------------------------------------------------
clear all
use year isocode type sector1 sector DA num_aff num_aff_ctry num_parents using "${output}/estimates_sec_naics_sales_exp_s4_base_exclexp_alt_all.dta", clear
rename DA xx
merge 1:1 year isocode type sector1 sector using "${output}/estimates_sec_naics_sales_s4_base_woparent.dta", keepusing(DA)
rename DA D_base
rename xx D_A

keep if type=="manuf" 
gen a=(D_base!=. &  D_A!=.)

by isocode year, sort: egen xx_baseline=count(D_base)
by isocode year, sort: egen xx_exp=total(a)
tab isocode if xx_baseline==xx_exp

gen dummy=0
foreach xx in DE EE FR GB GR HR HU SI {
display "`xx'"
replace dummy=1 if isocode=="`xx'"
}


clear all
use "${output}/estimates_aggtype_naics_sales_exp_s4_base_exclexp_alt_all.dta", clear
rename D_A xx
rename D_A_se xx_se
merge 1:1 year isocode type using "${output}/estimates_aggtype_naics_sales_s4_base_woparent.dta", keepusing(D_A D_A_se)
drop _merge
keep if type=="manuf"
rename D_A DA_base
rename D_A_se D_A_se_base
rename xx D_A
rename xx_se D_A_se

gen dummy=0
foreach xx in DE EE FR GB GR HR HU SI {
display "`xx'"
replace dummy=1 if isocode=="`xx'"
}
keep if dummy==1

gen DA_base_phi=(-1)*DA_base*(0.2)
gen DA_exp_phi=(-1)*D_A*(0.2)
gen DA_base_phi_se=(-1)*D_A_se_base*(0.2)
gen DA_exp_phi_se=(-1)*D_A_se*(0.2)

local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"
local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n,exp}"
global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter DA_base_phi DA_exp_phi, sort $opt1  mcolor(red)) (line DA_base_phi DA_base_phi, lcolor(red)) ,  ylabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) ///4
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 
*** before july 1 ****** graph export "${results}/figures/fig_exclexports_manufacturing.pdf", replace
***graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing.pdf", replace
***graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing_2.pdf", replace
*_2 refers to substracting exports in the numerator and denominator only when exports are non-missing and non-zero
*graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing_fvalt2.pdf", replace
graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing_fvalt3.pdf", replace


***Alternative figure with confidence intervals***	
gen u_DA_base = DA_base_phi + 1.96 * DA_base_phi_se 
gen l_DA_base = DA_base_phi - 1.96 * DA_base_phi_se 
gen u_DA = DA_exp_phi + 1.96 * DA_exp_phi_se 
gen l_DA = DA_exp_phi - 1.96 * DA_exp_phi_se 

keep year isocode type u_DA_base l_DA_base u_DA l_DA DA_base_phi DA_exp_phi
foreach vv in u_DA_base l_DA_base u_DA l_DA DA_base_phi DA_exp_phi {
display "`vv'"
}
gsort DA_base_phi
drop year
gen t=_n
sum t

sum t
forvalues ii=1(1)`r(max)' {
local ctry`ii' = isocode[`ii']
local obs`ii' = `ii'
display "`ctry`ii'' ; `obs`ii''"
}

twoway rcap u_DA_base l_DA_base t, lc(red) /// 
||     rcap u_DA l_DA t, lc(blue) /// 
||     scatter DA_base_phi t, mc(red)  /// 
||     scatter DA_exp_phi t, mc(blue)  ///
yline(0) yline(0) ylab(-0.8(0.2)0.2) xlab(1(1)8)  legend(off) xlabel(, valuelabel) xlabel(, angle(45)) ytitle("") xtitle("") graphregion(fcolor(white) lcolor(white)) xlabel(`obs1' "`ctry1'" `obs2' "`ctry2'"  `obs3' "`ctry3'" `obs4' "`ctry4'" `obs5' "`ctry5'" `obs6' "`ctry6'" `obs7' "`ctry7'" `obs8' "`ctry8'", labsize(small)) 
display "fig1_`title'_`yy'"
*graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing_fvalt2_b.pdf", replace
graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing_fvalt3_b.pdf", replace


/*
***Alternative figure with confidence intervals (DA instead of phi)***	
gen u_DA_base = DA_base + 1.96 * D_A_se_base 
gen l_DA_base = DA_base - 1.96 * D_A_se_base 
gen u_DA = D_A + 1.96 * D_A_se 
gen l_DA = D_A - 1.96 * D_A_se 

keep year isocode type u_DA_base l_DA_base u_DA l_DA DA_base D_A
foreach vv in u_DA_base l_DA_base u_DA l_DA DA_base D_A {
display "`vv'"
}
gsort DA_base
drop year
gen t=_n
sum t

sum t
forvalues ii=1(1)`r(max)' {
local ctry`ii' = isocode[`ii']
local obs`ii' = `ii'
display "`ctry`ii'' ; `obs`ii''"
}
*

twoway rcap u_DA_base l_DA_base t, lc(red) /// 
||     rcap u_DA l_DA t, lc(blue) /// 
||     scatter DA_base t, mc(red)  /// 
||     scatter D_A t, mc(blue)  ///
yline(0) yline(0) ylab(-2(1)4) xlab(1(1)8)  legend(off) xlabel(, valuelabel) xlabel(, angle(45)) ytitle("") xtitle("") graphregion(fcolor(white) lcolor(white)) xlabel(`obs1' "`ctry1'" `obs2' "`ctry2'"  `obs3' "`ctry3'" `obs4' "`ctry4'" `obs5' "`ctry5'" `obs6' "`ctry6'" `obs7' "`ctry7'" `obs8' "`ctry8'", labsize(small)) 
display "fig1_`title'_`yy'"
*/


*NEW Manufacturing Only: adjusting the baseline to have the same firms as the one excluding exports
*--------------------------------------------------------------------------
clear all
use "${output}/estimates_aggtype_naics_sales_exp_s4_base_exclexp_alt_all.dta", clear
rename D_A xx
rename D_A_se xx_se
merge 1:1 year isocode type using "${output}/estimates_aggtype_naics_sales_s4_base_check_all.dta", keepusing(D_A D_A_se)
drop _merge
keep if type=="manuf"
rename D_A DA_base
rename D_A_se D_A_se_base
rename xx D_A
rename xx_se D_A_se

gen dummy=0
foreach xx in DE EE FR GB GR HR HU SI {
display "`xx'"
replace dummy=1 if isocode=="`xx'"
}
keep if dummy==1

*gen DA_base_phi=(-1)*DA_base*(0.2)
*gen DA_exp_phi=(-1)*D_A*(0.2)
*gen DA_base_phi_se=(-1)*D_A_se_base*(0.2)
*gen DA_exp_phi_se=(-1)*D_A_se*(0.2)

gen DA_base_phi=(1)*DA_base
gen DA_exp_phi=(1)*D_A
gen DA_base_phi_se=(1)*D_A_se_base
gen DA_exp_phi_se=(1)*D_A_se

local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"
local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n,exp}"
global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter DA_base_phi DA_exp_phi, sort $opt1  mcolor(red)) (line DA_base_phi DA_base_phi, lcolor(red)) ,  ylabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) ///4
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 
*** before july 1 ****** graph export "${results}/figures/fig_exclexports_manufacturing.pdf", replace
***graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing.pdf", replace
***graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing_2.pdf", replace
*_2 refers to substracting exports in the numerator and denominator only when exports are non-missing and non-zero
**graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing_fvalt2_adj.pdf", replace


***Alternative figure with confidence intervals***	
gen u_DA_base = DA_base_phi + 1.96 * DA_base_phi_se 
gen l_DA_base = DA_base_phi - 1.96 * DA_base_phi_se 
gen u_DA = DA_exp_phi + 1.96 * DA_exp_phi_se 
gen l_DA = DA_exp_phi - 1.96 * DA_exp_phi_se 

keep year isocode type u_DA_base l_DA_base u_DA l_DA DA_base_phi DA_exp_phi
foreach vv in u_DA_base l_DA_base u_DA l_DA DA_base_phi DA_exp_phi {
display "`vv'"
}
gsort DA_base_phi
gen num=1 if isocode=="EE"
replace num=2 if isocode=="SI"
replace num=3 if isocode=="HR"
replace num=4 if isocode=="HU"
replace num=5 if isocode=="GR"
replace num=6 if isocode=="GB"
replace num=7 if isocode=="FR"
replace num=8 if isocode=="DE"
gsort -num

drop year
gen t=_n
sum t

sum t
forvalues ii=1(1)`r(max)' {
local ctry`ii' = isocode[`ii']
local obs`ii' = `ii'
display "`ctry`ii'' ; `obs`ii''"
}

twoway rcap u_DA_base l_DA_base t, lc(red) /// 
||     rcap u_DA l_DA t, lc(blue) /// 
||     scatter DA_base_phi t, mc(red)  /// 
||     scatter DA_exp_phi t, mc(blue)  ///
yline(0) yline(0) ylab(-2(1)4) xlab(1(1)8)  xlabel(, valuelabel) xlabel(, angle(45)) ytitle("") xtitle("") graphregion(fcolor(white) lcolor(white)) xlabel(`obs1' "`ctry1'" `obs2' "`ctry2'"  `obs3' "`ctry3'" `obs4' "`ctry4'" `obs5' "`ctry5'" `obs6' "`ctry6'" `obs7' "`ctry7'" `obs8' "`ctry8'", labsize(small)) legend(order(3 "Baseline (adjusted)" 4 "Export correction") rows(1) size(small) bplace(n) ring(0) region(lwidth(none)))
display "fig1_`title'_`yy'"
**graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing_fvalt2_adj_b.pdf", replace
graph export "${results}/figures/fig_exclexports_manufacturing_replacemissing_fvalt2_adj_c.pdf", replace


	
	

*Manufacturing Only: adjusting the baseline to have the same firms as the one excluding exports
*--------------------------------------------------------------------------
clear all
use year isocode type sector1 sector DA num_aff num_aff_ctry num_parents using "${output}/estimates_sec_naics_sales_exp_s4_base_exclexp_alt_all.dta", clear
rename DA xx
merge 1:1 year isocode type sector1 sector using "${output}/estimates_sec_naics_sales_s4_base_check_all.dta", keepusing(DA)
rename DA D_base
rename xx D_A

keep if type=="manuf" 
gen a=(D_A!=.)
keep if a==1
tab a if D_base==.
drop a
keep isocode year type sector1 sector
save "${output}/agg_index.dta", replace


clear all
use "${output}/estimates_aggtype_naics_sales_exp_s4_base_exclexp_alt_all.dta", clear
rename D_A xx
merge 1:1 year isocode type using "${output}/estimates_aggtype_naics_sales_s4_base_check_all.dta", keepusing(D_A)
drop _merge
keep if type=="manuf"
rename D_A DA_base
rename xx D_A

gen dummy=0
foreach xx in DE EE FR GB GR HR HU SI {
display "`xx'"
replace dummy=1 if isocode=="`xx'"
}
keep if dummy==1

gen DA_base_phi=(-1)*DA_base*(0.2)
gen DA_exp_phi=(-1)*D_A*(0.2)

local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"
local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n,exp}"
global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter DA_base_phi DA_exp_phi, sort $opt1  mcolor(red)) (line DA_base_phi DA_base_phi, lcolor(red)) ,  ylabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-0.8(0.2)0.2, format(%5.1f) labsize(medlarge)) ///4
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 
	
graph export "${results}/figures/fig_exclexports_manufacturing_overlapfirms.pdf", replace

	
	
	
*Exercise 4: using in the denominator only the largest 100 firms operating in each sector-isocode pair 
*-----------------------------------------------------------------
local number=100
use "${output}/estimates_agg_naics_sales_s4_base_woparent_orbis`number'_all.dta", clear

local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
	
sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local title "Affiliates older than `edad' years"
local title ""

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(-1(0.5)0.5, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0, format(%5.1f) labsize(medlarge)) ///
	title("`title'", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 
	
graph export "${results}/figures/fig_den`number'largestfirms.pdf", replace
	
	
*Compare phis agains the baseline
*-------------------------------------------------------
local number=20
clear all
use year isocode type D_A using "${output}/estimates_agg_naics_sales_s4_base_woparent_orbis`number'_all.dta", clear
merge 1:1 year isocode type using "${output}/estimates_database_v2.dta", keepusing(DA_base)
drop _merge

gen DA_base_phi=(-1)*DA_base*(0.2)
gen DA_top`number'firms_phi=(-1)*D_A*(0.2)

sum DA_base_phi DA_top`number'firms_phi
sum DA_base_phi DA_top`number'firms_phi if isocode!="MX"


local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}"
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n,top`number'firms}"
global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
two (scatter DA_top`number'firms_phi DA_base_phi, sort $opt1  mcolor(red)) (lfit DA_top`number'firms_phi DA_top`number'firms_phi, lcolor(red)) (line DA_top`number'firms_phi DA_top`number'firms_phi, lcolor(none)) ,  ylabel(-0.6(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-0.6(0.2)0.2, format(%5.1f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(off) 


	

	
	
*Figure 1: MNCs market share differences by income per capita of the location country (all economy)
*-------------------------------------------------------
clear all
use "${output}/estimates_agg_naics_sales_s4_base_check_all.dta", clear
rename D_A xx
merge 1:1 year isocode type using "${output}/estimates_agg_naics_sales_s4_base_woparent.dta", keepusing(D_A)
rename D_A D_base
rename xx D_A
drop _merge


local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

gen mkt_share=exp(D_A)
keep year isocode type mkt_share
tempfile temp_agg
save `temp_agg', replace


*Figure 1: MNCs market share differences by income per capita of the location country (manufacturing)
*-------------------------------------------------------
foreach tt in manuf serv others {
display "`tt'"

clear all
use "${output}/estimates_aggtype_naics_sales_s4_base_check_all.dta", clear
tab type
rename D_A xx
merge 1:1 year isocode type using "${output}/estimates_aggtype_naics_sales_s4_base_woparent.dta", keepusing(D_A)
drop _merge
rename D_A DA_base
rename xx D_A
by type, sort: sum DA_base D_A

keep if type=="`tt'"

local grvar0 "LP_ppp_emp_pwt" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

gen mkt_share=exp(D_A)
keep year isocode type mkt_share

tempfile temp_`tt'
save `temp_`tt'', replace
}

*Appending all files 
clear all
use `temp_agg'	
foreach tt in manuf serv others {
display "`tt'"
append using `temp_`tt''
}
compress
save "${output}/aff_mkt_shares.dta", replace
	








********************************************************** CHECKS ******************************************************************
	
	

************************
*This are the same 

clear all
use year isocode type sector1 sector DA num_aff num_aff_ctry num_parents using "${output}/estimates_sec_naics_sales_s4_base_check_all.dta", clear
rename DA xx
merge 1:1 year isocode type sector1 sector using "${output}/estimates_sec_naics_sales_s4_base_woparent.dta", keepusing(DA)
rename DA D_base
rename xx D_A
drop _merge

sum D_base D_A 
sum D_base D_A if D_A!=. & D_base!=.



************************

*Checking the baseline
*-----------------------------------------------------------------
clear all
use "${output}/estimates_agg_naics_sales_s4_base_check_all.dta", clear
rename D_A xx
merge 1:1 year isocode type using "${output}/estimates_agg_naics_sales_s4_base_woparent.dta", keepusing(D_A)
rename D_A D_base
rename xx D_A
drop _merge


local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
	
sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local title "Baseline"

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0, format(%5.1f) labsize(medlarge)) ///
	title("`title'", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 
	



*Checking the baseline (by type)
*-----------------------------------------------------------------
clear all
use "${output}/estimates_aggtype_naics_sales_s4_base_check_all.dta", clear
rename D_A xx
merge 1:1 year isocode type using "${output}/estimates_aggtype_naics_sales_s4_base_woparent.dta", keepusing(D_A)
drop _merge
rename D_A DA_base
rename xx D_A
*br year isocode type D_A DA_base if type=="others"
by type, sort: sum DA_base D_A

keep if type=="serv"

local grvar0 "LP_ppp_emp_pwt" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
	
sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local title "Baseline"

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0, format(%5.1f) labsize(medlarge)) ///
	title("`title'", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 
	






/*
**Figure 2 (just as a reference to recover the figure in the paper)***
*-----------------------------------------------------------------
global typeden=1
include "TFP_firm_directory_historical.do"
set memory 64g
global lf "LF"

use "${output}/estimates_database_v2.dta", clear
local cc  base_primenew
gen base=DA_base
gen D_A=DA_base

local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${output}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

local LHS1 D_`var3'_phi
local LHS2 b_D_`var3'_phi_`grvar0'
local RHS ln_`grvar0'
	
sum `LHS1' `RHS'  `LHS2'
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}, {&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"
local firm_emb "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:n}" 
local ctry_emb "{&Delta}`=ustrunescape("\u007A\u0303")'{sub:n}"
local title "Scenario 1: rho=`rho'"

reg `LHS1' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)

reg `LHS2' `RHS' 
mat b = e(b)
mat V = e(V)
global c2: display %-03.2fc round(b[1,1],0.01)
global se2: display %-03.2fc round(sqrt(V[1,1]),0.01)

display $c1
display $se1
display $c2
display $se2


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter `LHS1' `RHS', sort $opt1  mcolor(red)) (lfit `LHS1' `RHS', lcolor(red)) (line `RHS' `RHS', lcolor(none))  ///
	(scatter `LHS2' `RHS', sort $opt2  mcolor(blue)) (lfit `LHS2' `RHS', lcolor(blue)) (line `RHS' `RHS', lcolor(none)),  ylabel(, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0, format(%5.1f) labsize(medlarge)) ///
	title("`title'", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`firm_emb': $c1 ($se1)" 5 "`ctry_emb': $c2 ($se2)") rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

*/
	
	












































/*

*Aggregates (1): Compare the total revenue and employment in Orbis with the rev/emp reported in KLEMS. --- additional coverage figure
*------------------------------------------------------------------
clear all
*use year sector* isocode EMP* GO_usd GO_usd_exp VA_usd exports LAB_usd data_source using "${output}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear
use  "${output}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear


*****
use  "${output}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear


*****


*Step 1: keep only year 2016
keep if year==2016

*Step 2: 
*** Two option: national number directly from MNCs stats or using revenue from Klems / OECD  -- check how this change the numbers in some exercises in the paper 
gen Xfn=(GO_usd_mnc)/GO_usd_nt
sum Xfn, d
replace Xfn=. if Xfn>1 | Xfn<=0
gen Xnn=1-Xfn
drop Xfn

gen Xfn=(GO_usd_mnc)/GO_usd
sum Xfn, d
replace Xfn=. if Xfn>1 | Xfn<=0
gen Xnn2=1-Xfn
drop Xfn

sum Xnn Xnn2


************************************************************************************************************************


***I have to change this***
gen Xnn2=GO_usd
***I have to change***
rename VA_usd yn_va_ko
rename EMPE yn_emp_ko
rename exports yn_exp_ko
rename GO_usd yn_`var0'_ko
rename LAB_usd yn_labcost_ko
rename Xnn2 ynn_`var0'_ko
rename GO_usd_exp yn_`var0'_exp_ko
format %9.0fc yn_`var0'_ko ynn_`var0'_ko yn_`var0'_exp_ko yn_exp_ko yn_va_ko yn_labcost_ko
rename data_source data_source_0

*Now introduce the aggregates using KLEMS-NEW 
merge m:1 year sector isocode using "${output}/klems_oecd_comb_selectvar_v2.dta", keepusing(EMPE GO_usd LAB_usd COMP_usd data_source) 
drop if _merge==2 
drop _merge
rename EMPE yn_emp_ko_new
rename GO_usd yn_`var0'_ko_new
rename LAB_usd yn_labcost_ko_new
rename COMP_usd yn_comp_ko_new
format %9.0fc yn_emp_ko_new yn_`var0'_ko_new yn_labcost_ko_new yn_comp_ko_new
rename data_source data_source_1
local newlist yn_emp_ko_new yn_`var0'_ko_new yn_labcost_ko_new yn_comp_ko_new
display "`newlist'"


*Now introduce the aggregates using ORBIS 
merge m:1 year sector isocode using "${output}/orbis_historical_agg_allyears_allvariables.dta", keepusing(*_orbis_sawb)
drop if _merge==2 
drop _merge
*Renaming ORBIS variables 
rename GO_usd_orbis_sawb GO_usd_orbis
rename VA_usd_orbis_sawb VA_usd_orbis
rename EMP_orbis_sawb EMP_orbis
rename EXP_usd_orbis_sawb EXP_usd_orbis
rename LAB_usd_orbis_sawb LAB_usd_orbis


